#include "Operater.h"

#include <iostream>

#include "SGFem/Pre/Assembly/Assembly.h"
#include "SGFem/Pre/Assembly/CoordinateProcessor.h"

using std::vector;
using SG::DataStructure::Common::Real;
using namespace SG::Algebra;
using SG::DataStructure::Common::Id;
using SG::FEMSolver::CoordinateProcessor;


bool SG::FEMSolver::Element::calcMaterialOritation (SG::DataStructure::Common::Id                     mCId,
                            const SG::Algebra::Point&          central,
                            const SG::Algebra::Matrix&         T,
                            const vector<SG::DataStructure::Common::Real>& vectorG1G2,
                            SG::DataStructure::Common::Real                theta,
                            vector<SG::DataStructure::Common::Real>&       eleMatAxis)
{
    auto& coordHandler = CoordinateProcessor::GetInstance ();
    if (mCId > 0)
    {
        // 采用mcid方式：
        Matrix Tp;
        coordHandler.GetCoordTrans (mCId, central, _OUT Tp);
        Matrix xAxialGlobal (1, 3, { Tp (0, 0), Tp (0, 1), Tp (0, 2) });

        auto xAxialLocal = xAxialGlobal * T;
        Real norm        = sqrt (xAxialLocal (0, 0) * xAxialLocal (0, 0) + xAxialLocal (0, 1) * xAxialLocal (0, 1));
        eleMatAxis       = { xAxialLocal (0, 0) / norm, xAxialLocal (0, 1) / norm };
    }
    else
    {
        // 考虑转角theta方式
        Real norm = sqrt (vectorG1G2[0] * vectorG1G2[0] + vectorG1G2[1] * vectorG1G2[1]);
        // 将上述向量绕m_theta旋转得到材料主方向：

        Real c = cos (theta);
        Real s = sin (theta);
        // 材料主轴方向
        eleMatAxis = { (vectorG1G2[0] * c - vectorG1G2[1] * s) / norm, (vectorG1G2[0] * s + vectorG1G2[1] * c) / norm };
    }

    // 材料主轴转换标识，若需要转换(材料主轴与单元坐标系x轴不平行)，则为true,否为false
    return fabs (eleMatAxis[1]) > 1.0e-6;
}